<?php


namespace App\Http\Middleware;


use App\Utils\Output;
use Illuminate\Support\Facades\URL;

class AdminLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        if (!\App\Utils\Auth::userId()) {
            if ($request->ajax()) {
                return Output::error('登录过期',-100, ['jurl' => url('admin/public/login')])->toJson();
            } else {
                return redirect()->guest(url('admin/public/login') . '?jurl=' . urlencode(URL::full()));
            }
        }
//        list($controller, $action) = explode('@', Route::currentRouteAction());
//        if ($controller != 'App\Http\Controllers\Admin\IndexController' && !in_array($action, ['store', 'update'])) {
//            if (!\App\Utils\Auth::can($controller, $action)) {
//                if ($request->ajax()) {
//                    return Output::error('没有权限',-100, ['jurl' => url('admin/login')]);
//                } else {
//                    return response('没有权限', 401);
//                }
//            }
//        }
        return $next($request);
    }
}
